import { Document, Schema, model } from 'mongoose'

export type FriendStatus = 'pending' | 'accepted' | 'blocked'

interface IFriend extends Document {
  userId: string // 用户ID
  friendId: string // 好友ID
  group?: string // 分组名称
  status: FriendStatus // 好友状态
  createdAt: Date // 添加时间
  updatedAt: Date // 最后更新时间
}

const FriendSchema = new Schema<IFriend>({
  userId: { type: String, required: true, index: true },
  friendId: { type: String, required: true, index: true },
  group: String,
  status: {
    type: String,
    enum: ['pending', 'accepted', 'blocked'],
    default: 'pending',
  },
  createdAt: { type: Date, default: Date.now },
  updatedAt: { type: Date, default: Date.now },
})

// 复合唯一索引，防止重复添加
FriendSchema.index({ userId: 1, friendId: 1 }, { unique: true })

export { IFriend }
export const Friend = model<IFriend>('Friend', FriendSchema)
